/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mysweethome.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import mysweethome.entities.Customer;

/**
 *
 * @author johan
 */
public class CustomerDAO extends AbstractBaseDAO<Customer> {

    private Query query;

    public CustomerDAO(EntityManager em) {
        super(em);
    }

    /**
     * 
     * @param username : logon name
     * @param password : password
     * @return TRUE if username or password is correct
     */
    public boolean checkLogin(String username, String password) {
        //em.getTransaction().begin();

        query = em.createNamedQuery("Customer.findByUsernamePassword");
        query.setParameter("username", username);
        query.setParameter("password", password);
        try {
            Customer customer = (Customer) query.getSingleResult();
            return true;
        } catch (NoResultException noresult) {
            return false;
        } catch (RuntimeException ex) {
            ex.printStackTrace();
            return false;
        }
    }
    /**
     * Select All
     * output:List customer
     */
    public List<Customer> SelectAll()
    {
        List<Customer> list=new ArrayList<Customer>();
        try{
        query = em.createNamedQuery("Customer.findAll");
        list=query.getResultList();
        }catch(NoResultException noresult){
            noresult.printStackTrace();
            return null;
        }catch(RuntimeException ex){
            ex.printStackTrace();
            return null;
        }
        return list;
    }
    /**
     * 
     * @param id
     * @return List Customer
     */
    public List<Customer> SearchByID(String id)
    {
        List<Customer> list=new ArrayList<Customer>();
        try{
        query = em.createNamedQuery("Customer.findByCustomerID");
        query.setParameter("customerID", id);
        list=query.getResultList();
        }catch(NoResultException noresult){
            noresult.printStackTrace();
            return null;
        }catch(RuntimeException ex){
            ex.printStackTrace();
            return null;
        }
        return list;
    }
}
